﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Linq.Expressions;

namespace EntityFrameworkDemo.DataAccess
{
    public class TempDB1
    {
        public TempDB1Entities6 db;

        public TempDB1()
        {
            db = new TempDB1Entities6();
            db.Users.Where(x=>x.Age == x.Age);
            var ds = db.Set<User>();
        }

        public int Add<T>(T Model)
            where T : class
        {
            DbSet dst = db.Set<T>();
            dst.Add(Model);
            return db.SaveChanges();
        }

        public int Del<T>(T Model)
            where T : class
        {
            DbSet dst = db.Set<T>();
            dst.Attach(Model);
            dst.Remove(Model);
            return db.SaveChanges();
        }

        /// <summary>
        /// 根据条件删除(支持批量删除)
        /// </summary>
        /// <param name="delWhere">传入Lambda表达式(生成表达式目录树)</param>
        /// <returns></returns>
        public int DelBy<T>(Expression<Func<T, bool>> delWhere)
            where T : class
        {
            List<T> listDels = db.Set<T>().Where(delWhere).ToList();
            listDels.ForEach(d =>
            {
                // db.Set<T>().Attach(d);
                db.Set<T>().Remove(d);
            });
            return db.SaveChanges();
        }

        #region 查询

        public void Method1()
        {
            Expression<Func<int, int, int>> exp2 = (m, n) => m * n + 2;

        }

        #endregion 
    }
}
